import { NInputNumber } from "naive-ui";
import { getSlotsDom } from "../utils";
import type { InputNumberProps, InputNumberInst } from "naive-ui";
import type { BaseComponentType, OmitHTMLAttributes } from "../types";

export type InputNumberFactoryConstructorType = {
  props?: InputNumberProps;
  attrs?: OmitHTMLAttributes<"onChange">;
  addIconSlot?: BaseComponentType;
  minusIconSlot?: BaseComponentType;
  prefixSlot?: BaseComponentType;
  suffixSlot?: BaseComponentType;
};
export class InputNumberFactory {
  private attrs: OmitHTMLAttributes<"onChange"> = {};
  private props: InputNumberProps = {};
  private addIconSlot: BaseComponentType = null;
  private minusIconSlot: BaseComponentType = null;
  private prefixSlot: BaseComponentType = null;
  private suffixSlot: BaseComponentType = null;
  private domRef: InputNumberInst | null = null;

  constructor(data?: InputNumberFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.addIconSlot) this.setAddIcon(data.addIconSlot);
    if (data?.minusIconSlot) this.setMinusIcon(data.minusIconSlot);
    if (data?.prefixSlot) this.setPrefix(data.prefixSlot);
    if (data?.suffixSlot) this.setSuffix(data.suffixSlot);
  }
  setAttrs(attrs: OmitHTMLAttributes<"onChange">) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: InputNumberProps) {
    this.props = props;
    return this;
  }
  setAddIcon(component: BaseComponentType) {
    this.addIconSlot = component;
    return this;
  }
  setMinusIcon(component: BaseComponentType) {
    this.minusIconSlot = component;
    return this;
  }
  setPrefix(component: BaseComponentType) {
    this.prefixSlot = component;
    return this;
  }
  setSuffix(component: BaseComponentType) {
    this.suffixSlot = component;
    return this;
  }
  blur() {
    this.domRef?.blur();
  }

  focus() {
    this.domRef?.focus();
  }
  create() {
    return (
      <NInputNumber
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as InputNumberInst;
        }}
        {...this.attrs}
        {...this.props}
      >
        {{
          "add-icon": () => getSlotsDom(this.addIconSlot),
          "minus-icon": () => getSlotsDom(this.minusIconSlot),
          prefix: () => getSlotsDom(this.prefixSlot),
          suffix: () => getSlotsDom(this.suffixSlot),
        }}
      </NInputNumber>
    );
  }
}
